import React, { useEffect } from 'react'
import { connect } from 'react-redux'

function Home (props) {
  const { bannerList, proList } = props
  // 函数式组件没有生命周期钩子函数，但是react 16.8 版本时推出了 hooks
  // useEffect(() => {}, []) ===> componentDidMount
  useEffect(() => {
    props.getBannerList()
    props.getProList()
  }, [])
  return (
    <div>
      {
        bannerList && bannerList.map(item => (
          <img key = { item.bannerid } src={ item.img } alt={ item.alt }/>
        ))
      }
      {
        proList && proList.map(item => (
          <div key = { item.proid }>{ item.proname }</div>
        ))
      }
    </div>
  )
}
export default connect(
  ({ bannerList, proList }) => ({bannerList, proList}),
  (dispatch) => ({
    getBannerList () {
      // ajax jquery fetch axios
      fetch('http://121.89.205.189/api/banner/list')
        .then(res => res.json())
        .then(res => {
          console.log(res)
          dispatch({
            type: 'CHANGE_BANNER_LIST',
            payload: res.data
          })
        })
    },
    getProList () {
      fetch('http://121.89.205.189/api/pro/list')
        .then(res => res.json())
        .then(res => {
          console.log(res)
          dispatch({
            type: 'CHANGE_PRO_LIST',
            payload: res.data
          })
        })
    }
  }))(Home)